#include "myhead.h"
#include "ts.h"
#include "lcd.h"
#include "login.h"
#include "music.h"
#include "video.h"
#include "photograph.h"


int *lcd_mmap;
int lcd_fd;
int ts_fd;

int main(int argc, char const *argv[])
{
	lcd_fd = lcd_init();
	ts_fd  = ts_init();

	show_open(2);
    show_login();
	usleep(800*1000);
	desktop_refresh(0);

	struct coordinate old_coor;		//触摸屏坐标
	struct coordinate new_coor;
	bool released;					//弹起标志
	bool firsttouch = true;			//第一手接触
	while(1)
	{
		ts_trace(ts_fd, &old_coor, &released);

		printf("x = %d, y = %d, released = %d\n",
						 old_coor.x, old_coor.y, released);
		//判断划屏操作
		if (released==false && old_coor.y>420 && firsttouch)
		{
			int x = old_coor.x;
			while(!released)
			{
				ts_trace(ts_fd, &new_coor, &released);
				if (new_coor.x == old_coor.x)
					continue;

				float angle = (float)(new_coor.y-old_coor.y)
										/(new_coor.x-old_coor.x);
				printf("[new] x = %d, y = %d, angle = %f\n",
								 new_coor.x, new_coor.y, angle);

				if (angle > TAN30 || angle < -TAN30)
					break;
				dock_refresh(new_coor.x - x);
				x = new_coor.x;
			}
		}

		firsttouch = false;
		if (released)
		{
			if (old_coor.x>160&&old_coor.x<220
				&&old_coor.y>330&&old_coor.y<390)
			{
				load_join(40);
				choose_game();
				load_join(40);
				desktop_refresh(0);
			}	
			if (old_coor.x>20&&old_coor.x<80
				&&old_coor.y>90&&old_coor.y<150)
			{
				load_join(40);
				temperature();
				load_join(40);
				desktop_refresh(0);
			}	
			if (old_coor.x>90&&old_coor.x<150
				&&old_coor.y>90&&old_coor.y<150)
			{
				load_join(40);
				photo_album();
				load_join(40);
				desktop_refresh(0);
			}	
			if (old_coor.x>90&&old_coor.x<150
				&&old_coor.y>270&&old_coor.y<330)
			{
				load_join(40);
				music_album();
				load_join(40);
				desktop_refresh(0);
			}	
			if (old_coor.x>160&&old_coor.x<220
				&&old_coor.y>90&&old_coor.y<150)
			{
				load_join(40);
				video_list();
				load_join(40);
				desktop_refresh(0);
			}	
			
			if (old_coor.x>650&&old_coor.x<800
				&&old_coor.y>420&&old_coor.y<480)
			{
				load_join(40);
				take_picture();
				load_join(40);
				desktop_refresh(0);
			}	
			
			

			
			firsttouch = true;
		}
	}
	lcd_close();
	return 0;
}

